//
//  UserLoginModel.m
//  SixFeetLanePro
//
//  Created by JinLiang on 14/11/12.
//  Copyright (c) 2014年 QCSH. All rights reserved.
//

#import "UserLoginModel.h"
#import "LoginModel.h"
//#import "LoginApIModel.h"
@implementation UserLoginModel

//判断用户是否登录
+ (BOOL)isLogged
{
    NSString *sessionId =[[CommClass sharedCommon] objectForKey:LOGIN_SESSIONID];
    
    if (!sessionId||[sessionId isEqualToString:@""]) {
        return NO;
    }else{
        return YES;
    }
}

//归档用户信息到本地
+ (void)setLoginWithUserName:(NSString *)userName passWord:(NSString *)passWord userInfo:(NSDictionary *)userInfo
{
    // 过滤 null
    NSMutableDictionary *mutableInfo = [NSMutableDictionary dictionary];
    
    for (NSString *key in userInfo) {
        NSString *value = [userInfo objectForKey:key];
        
        if ([DataCheck isValidNumber:value]) {
            value=[NSString stringWithFormat:@"%@",value];
        }
        if (![DataCheck isValidString:value] &&![DataCheck isValidArray:value]) {
            value = @"";
        }
        [mutableInfo setObject:value forKey:key];
    }
    userInfo = (NSDictionary *)mutableInfo;
    //生成对象
    LoginModel *personInfo=[LoginModel mj_objectWithKeyValues:userInfo];
    //写入用户信息到内存中
    [[CommClass sharedCommon] setObject:userName forKey:LOGIN_USERNAME];
    [[CommClass sharedCommon] setObject:personInfo.PersonID forKey:LOGIN_SESSIONID];
    [[CommClass sharedCommon] setObject:passWord forKey:LOGIN_PASSWORD];
    [[CommClass sharedCommon] setObject:personInfo.PersonName forKey:LOGIN_PERSONNAME];
    [[CommClass sharedCommon] setObject:personInfo.power forKey:LOGIN_USERPOWER];
    NSMutableArray *contentMuArray=[[NSMutableArray alloc]init];
    NSString *plistPath=[self getFilePath:@"coding.plist"];
    personInfo.loginName = userName;
    personInfo.passWord = passWord;
    personInfo.isRemeberPwd = [[[CommClass sharedCommon] objectForKey:ISREMEBERPWD] boolValue];
    personInfo.isAutoLogin = [[[CommClass sharedCommon] objectForKey:ISAUTOLOGIN] boolValue];
    //创建用于包含编码的person对象的data实例
    NSMutableData *data=[[NSMutableData alloc]init];
    //创建编码对象，提前告诉它，谁用来装载数据的
    NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
    [contentMuArray addObject:personInfo];
    //进行编码
    [archiver encodeObject:contentMuArray forKey:@"Data"];
    //结束归档
    [archiver finishEncoding];
    //写入数据
    [data writeToFile:plistPath atomically:YES];
    

}


+(NSString *)getFilePath:(NSString *)fileName
{
    //取得document的路径
    NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
    //拼接要存储的plist路径
    NSString *plistPath=[docPath stringByAppendingPathComponent:fileName];
    return plistPath;
}

//自动登录
+ (void)setAutoLogin
{
   
    NSString *plistPath=[self getFilePath:@"coding.plist"];
    //获取解码内容
    NSData *data=[[NSData alloc]initWithContentsOfFile:plistPath];
    //创建解码对象
    if (data !=nil) {
        NSKeyedUnarchiver *unArchive=[[NSKeyedUnarchiver alloc]initForReadingWithData:data];
        //从解码对象中获得解码内容
        NSMutableArray *personArray=[unArchive decodeObjectForKey:@"Data"];
        LoginModel *person=[personArray objectAtIndex:random()%personArray.count];
        [unArchive finishDecoding];
        //生成对象
//        LoginModel *personInfo=[LoginModel mj_objectWithKeyValues:person.userInfo];
        //写入用户信息到内存中
        if (person.isAutoLogin) {
            [[CommClass sharedCommon] setObject:person.PersonID forKey:LOGIN_SESSIONID];
            [[CommClass sharedCommon] setObject:person.PersonName forKey:LOGIN_PERSONNAME];
            [[CommClass sharedCommon] setObject:person.power forKey:LOGIN_USERPOWER];
        }
        [[CommClass sharedCommon] setObject:person.loginName forKey:LOGIN_USERNAME];
        [[CommClass sharedCommon] setObject:person.passWord forKey:LOGIN_PASSWORD];
        [[CommClass sharedCommon] setObject:@(person.isRemeberPwd) forKey:ISREMEBERPWD];
        
    }
    
}

//退出
+ (void)setLoginOut
{
    [[CommClass sharedCommon] removeObjectForKey:LOGIN_SESSIONID];
    [[CommClass sharedCommon] removeObjectForKey:LOGIN_PASSWORD];
    [[CommClass sharedCommon] removeObjectForKey:LOGIN_PERSONNAME];
    [[CommClass sharedCommon] removeObjectForKey:LOGIN_USERPOWER];
    [[CommClass sharedCommon] removeObjectForKey:ISAUTOLOGIN];
    NSMutableArray *contentMuArray=[[NSMutableArray alloc]init];
    NSString *plistPath=[self getFilePath:@"coding.plist"];
    //生成对象
    LoginModel *person=[[LoginModel alloc]init];
    person.loginName = [[CommClass sharedCommon] objectForKey:LOGIN_USERNAME];
    //创建用于包含编码的person对象的data实例
    NSMutableData *data=[[NSMutableData alloc]init];
    //创建编码对象，提前告诉它，谁用来装载数据的
    NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
    [contentMuArray addObject:person];
    //进行编码
    [archiver encodeObject:contentMuArray forKey:@"Data"];
    //结束归档
    [archiver finishEncoding];
    //写入数据
    [data writeToFile:plistPath atomically:YES];

}
/**获取用户id*/
+ (NSString *)getSessionID{
    return [[CommClass sharedCommon] objectForKey:LOGIN_SESSIONID];
}
/**获取登录用户权限*/
+ (NSString *)getPower{
    return [[CommClass sharedCommon] objectForKey:LOGIN_USERPOWER];
}
/**获取登录用户名*/
+ (NSString *)getUsername
{
    return [[CommClass sharedCommon] objectForKey:LOGIN_PERSONNAME];
}
@end
